package com.jeff.android.lab.architecture.arouter.interceptor;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jeff.android.lab.commonlib.router.Constant;
import com.jeff.utils.LogUtil;

import static com.jeff.android.lab.commonlib.router.Constant.GROUP_LOGGED_IN;

/**
 * @author: duff
 * @date: 2019/3/14
 * @since: 1.0.0
 */
@Interceptor(priority = 3, name = "登录拦截器")
public class LoginInterceptor implements IInterceptor {

    private boolean isLogin = true;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        final String group = postcard.getGroup();
        if (GROUP_LOGGED_IN.equals(group)) { // 路由组为 logged_in 的需要检查登录态
            if (isLogin) {
                callback.onContinue(postcard);
            } else {
                ARouter.getInstance()
                        .build(Constant.ACTIVITY_URL_LOGIN)
                        .navigation();

                callback.onInterrupt(new RuntimeException("This is need to login first."));
            }
        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
        LogUtil.d("dingfeng", "LoginInterceptor init.");
    }

}
